package com.ym.controller.common.jpush;

import com.ym.common.constant.Constants;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.core.utils.JPushUtil;
import com.ym.core.utils.tkmybatis.TK;
import com.ym.medical.domain.bo.PushBean;
import com.ym.medical.domain.entity.JpushAccountEntity;
import com.ym.medical.service.IJpushAccountSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * 极光推送服务
 */
@Api(tags = "极光推送")
@RestController
@RequestMapping(value = "/common/jpush", headers = Constants.Api.VERSION)
public class PushController extends BaseController {

	@Autowired
	private IJpushAccountSV jpushAccountSV;

	/**
	 * 添加极光账号
	 *
	 * @return
	 */
	@Log
	@ApiOperation("添加极光账号")
	@PostMapping(value = "/account/add", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse accountAdd(@RequestBody JpushAccountEntity pushAccountModel) {
		Long loginId = ShiroUtil.getLoginId();
		pushAccountModel.setUserId(loginId);
		pushAccountModel.setCreateTime(new Date());
		int count = jpushAccountSV.updateByParams(pushAccountModel,
				TK.selectWhere(JpushAccountEntity.class)
						.andEqualTo(JpushAccountEntity::getUserId, loginId)
						.andEqualTo(JpushAccountEntity::getType, pushAccountModel.getType())
						.end()
		);
		if (count == 0) {
			jpushAccountSV.insert(pushAccountModel);
		}
		return success();
	}

	/**
	 * 推送全部(包括ios和android)
	 *
	 * @param pushBean
	 * @return
	 */
	@Log
	@ApiOperation("推送全部(包括ios和android)")
	@PostMapping(value = "/all", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse pushAll(@RequestBody PushBean pushBean) {
		return successData(JPushUtil.pushAll(pushBean));
	}

	/**
	 * 推送全部ios
	 *
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送全部ios")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "extras", value = "额外字段,JSON字符串KEY VALUE格式", required = false, paramType = "body")
	})
	@PostMapping(value = "/ios/all", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse pushIos(@RequestBody PushBean pushBean) {
		return successData(JPushUtil.pushIos(pushBean));
	}

	/**
	 * 推送指定ios
	 *
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送指定ios")
	@PostMapping(value = "/ios", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse pushIos(@RequestBody PushBean pushBean, @RequestParam String[] registerids) {
		return successData(JPushUtil.pushIos(pushBean, registerids));
	}

	/**
	 * 推送全部android
	 *
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送全部android")
	@PostMapping(value = "/android/all", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse pushAndroid(@RequestBody PushBean pushBean) {
		return successData(JPushUtil.pushAndroid(pushBean));
	}

	/**
	 * 推送指定android
	 *
	 * @param pushBean
	 * @return
	 */
	@ApiOperation("推送指定android")
	@PostMapping(value = "/android", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse pushAndroid(@RequestBody PushBean pushBean, @RequestParam String[] registerids) {
		return successData(JPushUtil.pushAndroid(pushBean, registerids));
	}

}
